﻿
Public Class operate
    Public Const functionName As String = "op"

    Private Shared Sub normalizeOperOperOverride(ByVal oper As String, ByRef operOverride As String)
        If isemptystring(operOverride) Then
            operOverride = oper
        End If
    End Sub

    Public Shared Sub register(ByVal inlineScript As inlineScript, ByVal oper As String, _
                               Optional ByVal operOverride As String = Nothing)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        normalizeOperOperOverride(oper, operOverride)
        inlineScript.addscript(network.http.contentType.textjavascript, "operator_" + oper + "_" + operOverride, _
                               "function " + functionName + "_" + operOverride + "(i,p)" + _
                               "{i." + oper + "=p;}")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal oper As String, _
                                  ByVal dest As String, ByVal para As String, _
                                  Optional ByRef actionTarget As String = Nothing, _
                                  Optional ByVal operOverride As String = Nothing) As String
        'for ajax, the server may not response a full page, but the original page contains required javascript already.
        'assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        assert(Not isemptystring(dest), "dest is an empty string.")
        assert(Not isemptystring(oper), "oper is an empty string.")
        assert(Not isemptystring(para), "para is an empty string.")
        normalizeOperOperOverride(oper, operOverride)
        If Not inlineScript Is Nothing Then
            register(inlineScript, oper, operOverride)
        End If

        actionTarget += functionName + "_" + operOverride + "(" + dest + "," + para + ")"
        Return actionTarget
    End Function
End Class

Public Class changeValue
    Public Const oper As String = "value"
    Public Const operOverride As String = "cv"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        operate.register(inlineScript, oper, operOverride)
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal i As String, ByVal value As Object, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        Return operate.invoke(inlineScript, oper, i, Convert.ToString(value), actionTarget, operOverride)
    End Function
End Class

Public Class visibility
    Public Const oper As String = "style.visibility"
    Public Const operOverride As String = "vis"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        operate.register(inlineScript, oper, operOverride)
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal target As String, _
                                  ByVal display As Boolean, Optional ByRef actionTarget As String = Nothing) As String
        Return operate.invoke(inlineScript, oper, target, _
                              ifoper(display, "'visible'", "'hidden'"), actionTarget, operOverride)
    End Function

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal target As String, _
                                  ByVal value As String, Optional ByRef actionTarget As String = Nothing) As String
        Return operate.invoke(inlineScript, oper, target, value, actionTarget, operOverride)
    End Function
End Class

Public Class display
    Public Const oper As String = "style.display"
    Public Const operOverride As String = "dsp"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        'do not directly call inlineScript, so do not assert here
        'assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        operate.register(inlineScript, oper, operOverride)
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal target As String, _
                                  ByVal displayStyle As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If
        Return operate.invoke(inlineScript, oper, target, displayStyle, actionTarget, operOverride)
    End Function
End Class
